package com.peakmain.wanandroid.netmodule.net.http.params;

import com.peakmain.wanandroid.netmodule.net.http.callback.RequestCallBack;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * author ：Peakmain
 * version : 1.0
 * createTime：2020/2/22
 * mail:2726449200@qq.com
 * describe： post 请求实现类
 */
public class PostRequest extends BaseRequest<PostRequest> {

    public static final MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8");
    public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json;charset=utf-8");
    public static final MediaType MEDIA_TYPE_STREAM = MediaType.parse("application/octet-stream");

    private MediaType mediaType; //上传的MIME类型
    private String string;       //上传的文本内容
    private String json;         //上传的Json
    private byte[] bs;           //上传的字节数据

    public PostRequest() {
        super();
    }

    /**
     * 注意使用该方法上传字符串会清空实体中其他所有的参数，头信息不清除
     */
    public PostRequest postString(String string) {
        this.string = string;
        this.mediaType = MEDIA_TYPE_PLAIN;
        return this;
    }

    /**
     * 注意使用该方法上传字符串会清空实体中其他所有的参数，头信息不清除
     */
    public PostRequest postJson(String json) {
        this.json = json;
        this.mediaType = MEDIA_TYPE_JSON;
        return this;
    }

    /**
     * 注意使用该方法上传字符串会清空实体中其他所有的参数，头信息不清除
     */
    public PostRequest postBytes(byte[] bs) {
        this.bs = bs;
        return this;
    }

    public PostRequest mediaType(MediaType mediaType) {
        this.mediaType = mediaType;
        return this;
    }

    @Override
    protected RequestBody generateRequestBody() {
        if (string != null && mediaType != null)
            return RequestBody.create(mediaType, string);    //post上传字符串数据
        if (json != null && mediaType != null)
            return RequestBody.create(mediaType, json);       //post上传json数据
        if (bs != null && mediaType != null)
            return RequestBody.create(mediaType, bs);         //post上传字节数组
        return generateMultipartRequestBody();
    }

    @Override
    protected Request buildRequest(RequestBody requestBody) {
        Request.Builder requestBuilder = new Request.Builder();
        try {
            headers.put("Content-Length", String.valueOf(requestBody.contentLength()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        appendHeaders(requestBuilder);
        return requestBuilder.post(requestBody).url(url).tag(tag).build();
    }

    @Override
    protected RequestBody wrapRequestBody(RequestBody requestBody, final RequestCallBack callback) {

        if(callback == null) return requestBody;
        //如果文件列表为空则取原来的
        if( mParams.fileParamsMap.isEmpty())return requestBody;
        ProgressRequestBody progressRequestBody =new ProgressRequestBody(requestBody, new ProgressRequestBody.UploadInterceptor() {
            @Override
            public void onRequestProgress(long bytesWritten, long contentLength) {
                callback.onProgress((int) ((100 * bytesWritten) / contentLength));
            }
        });
        return progressRequestBody;
    }
}
